package com.android.webserver.tornado;

import java.net.*;
import java.io.IOException;

public class ListenThread extends Thread
{
	private final ServerPool	serverPool;
	private final int					port;
	private ServerSocket			serverSocket;

	/** Constructs a new thread with the specified values. */
	public ListenThread(ServerPool serverPool, int port)
	{
		this.serverPool = serverPool;
		this.port = port;
	}

	/**
	 * Begins an infinite loop, accepting and dispatching incoming connections.
	 */
	public void run()
	{
		bindToPort();
		while (true)
		{
			listen();
		}
	}

	/**
	 * Connects Tornado to a local system port. If an error is encountered,
	 * Tornado aborts.
	 */
	private void bindToPort()
	{
		try
		{
			serverSocket = new ServerSocket(port);
		}
		catch (IOException e)
		{
			// treat all I/O errors as fatal
			throw new RuntimeException(e.getMessage());
		}
	}

	/** Waits for, accepts and dispatches a single incoming connection. */
	private void listen()
	{
		try
		{
			Socket connection = serverSocket.accept();
			serverPool.dispatch(connection);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
